package com.comdo.newup_loan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.model.IndustryBean;

import java.util.ArrayList;

/**
 * Created by liyuan on 2017/9/27.
 */

public class IndustryAdapter extends RecyclerView.Adapter<IndustryAdapter.ViewHolder> implements View.OnClickListener {

    public ArrayList<IndustryBean> datas = null;
    private OnItemClickListener mOnItemClickListener = null;
    private Context context;

    public IndustryAdapter(ArrayList<IndustryBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        viewHolder.itemView.setTag(position);
        viewHolder.mIndustryText.setText(datas.get(position).getIndustryName());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_industry, parent, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mIndustryText;

        public ViewHolder(View view) {
            super(view);
            mIndustryText = (TextView) view.findViewById(R.id.text);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
